
package com.creditratingmodule.servlet;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UtilDAO;

import com.creditratingmodule.bean.TbCreditRating;
import com.creditratingmodule.bean.TbCreditRatingType;
import com.creditratingmodule.dao.TbCreditRatingDAO;
import com.creditratingmodule.dao.TbCreditRatingTypeDAO;

public class TbCreditRatingServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        int creditRatingTypeId=Integer.parseInt(request.getParameter("creditRatingTypeId"));
        String creditRatingScore=request.getParameter("creditRatingScore");
            if(new TbCreditRatingDAO().add(new TbCreditRating(creditRatingTypeId,creditRatingScore))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
           goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_CreditRating", "CreditRatingId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbCreditRatingList",new TbCreditRatingDAO().getTbCreditRatingAll());
            response.sendRedirect("CreditratingModule/TbCreditRatingList.jsp");
        }
        if("edit".equals(method)){
        int creditRatingId=Integer.parseInt(request.getParameter("creditRatingId"));
        int creditRatingTypeId=Integer.parseInt(request.getParameter("creditRatingTypeId"));
        String creditRatingScore=request.getParameter("creditRatingScore");
            if(new TbCreditRatingDAO().edit(new TbCreditRating(creditRatingId,creditRatingTypeId,creditRatingScore))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method)) goList(request, response);
        if ("listById".equals(method)) {
        	 request.getSession(true).setAttribute("tbCreditRatingTypeList",new TbCreditRatingTypeDAO().getTbCreditRatingTypeAll());
             request.getSession(true).setAttribute("tbCreditRating",new TbCreditRatingDAO().getTbCreditRatingByCreditRatingId(Integer.parseInt(request.getParameter("creditRatingId"))));
            response.sendRedirect("CreditratingModule/TbCreditRatingOperate.jsp");
    	}
        if("showadd".equals(method)){
        	 request.getSession(true).setAttribute("tbCreditRatingTypeList",new TbCreditRatingTypeDAO().getTbCreditRatingTypeAll());
            request.getSession().setAttribute("creditRatingTypeList",new TbCreditRatingTypeDAO().getTbCreditRatingTypeAll());
            response.sendRedirect("CreditratingModule/TbCreditRatingOperate.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbCreditRatingList",new TbCreditRatingDAO().getTbCreditRatingAll());
        response.sendRedirect("CreditratingModule/TbCreditRatingList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
